<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script type="text/javascript" src="./uni.webview.1.5.4.js"></script>
  <title></title>
  <style>
    body {
      background-color: #f5f5f5;
    }

    #placeholder {
      position: fixed;
      top: 50%;
      right: 0;
      bottom: 0;
      left: 50%;
      transform: translate(-50%, -50%);
      height: 30px;
      line-height: 30px;
      text-align: center;
    }
  </style>
</head>

<body>
  <div id="placeholder">加载中...</div>
</body>
<script>
  const fromEntries = (arrArg) => {
    // Map
    if (Object.prototype.toString.call(arrArg) === "[object Map]") {
      const resMap = {};
      for (const key of arrArg.keys()) {
        resMap[key] = arrArg.get(key);
      }
      return resMap;
    }
    // array
    if (Array.isArray(arrArg)) {
      const resArr = {}
      arrArg.map(([key, value]) => {
        resArr[key] = value
      })
      return resArr
    }
    throw "Uncaught TypeError: argument is not iterable";
  }

  const transformQueryString = params => {
    if (typeof params === "string") {
      const queryStrings = params.match(/[^?&]+=[^?&]+/g);

      return queryStrings ? fromEntries(
        queryStrings.map(item => item.split(/^([^=]*)=*/).filter(Boolean))
      ) : {};
    }
    if (typeof params === "object") {
      return Object.keys(params).filter(key => params[key]).map(key => `${key}=${params[key]}`).join("&");
    }
    alert("Parameter error");
  }

  document.addEventListener("DOMContentLoaded", () => {
    const { hash: url, search } = location,
      { title, params } = transformQueryString(search);

    document.title = title ? decodeURIComponent(title) : "星球崛起";

    if (url) {
      fetch(url.slice(1) + (params ? transformQueryString(JSON.parse(decodeURIComponent(params))) : ""))
        .then(res => res.text())
        .then((res) => {
          try {
            const { data, code, msg } = JSON.parse(res);
            if (code !== 1) {
              alert(msg);
              return;
            }
            document.body.innerHTML = data.content || data.detail;
          } catch (error) {
            document.body.innerHTML = res
          }
        });
      return;
    }

    document.addEventListener("UniAppJSBridgeReady", () => {
      uni.getEnv((res) => {
        function innerHTML(env = "default") {
          const richText = {
            h5: () => localStorage.getItem("richText"),
            plus: () => plus.storage.getItem("richText"),
            default: () => `<div id="placeholder">暂未实现...</div>`
          }[env]();
          richText && (document.body.innerHTML = richText);
        }
        switch (true) {
          case res.h5:
            innerHTML("h5");
            break;
          case res.plus:
            (window.plus ? innerHTML("plus") : document.addEventListener("plusready", () => innerHTML("plus")));
            break;
          default:
            innerHTML();
            break;
        }
      });
    });
  }, true);

</script>

</html>